package com.tjetc.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/9/8
 */

// 一定要将SpringMVC容器和Spring容器分开设置扫描 防止出现重复扫描 注入重复bean的情况
@ComponentScan("com.tjetc.test.controller")
// 开始SpringMVC注解功能
@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

    // 配置我们的视图解析器
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
       // 配置jsp的视图解析器
        /*
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!---->
            <property name="prefix" value="/view/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
         */
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/view/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }


    // 配置我们的拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*
             <mvc:interceptor>
                <!--拦截所有/interceptor/f* 以f开发的路径-->
                <mvc:mapping path="/interceptor/f*"/>
                <!--排除拦截的路径-->
                <mvc:exclude-mapping path="/interceptor/f1"/>
                <!--使用哪个bean来进行拦截-->
                <ref bean="myInterceptor"/>
             </mvc:interceptor>
         */
//        registry.addInterceptor(new MyInterceptor())
//                .addPathPatterns("/f*")
//                .excludePathPatterns("/f1")
//                .order(1);

    }

    // json编码


    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        WebMvcConfigurer.super.extendMessageConverters(converters);
        converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
    }

    // 配置上传下载
    // todo 未生效
    // 2. 配置 Spring 的 MultipartResolver，它只是一个代理
    @Bean(name = "multipartResolver") // Bean 的名称必须是 'multipartResolver'
    public StandardServletMultipartResolver standardServletMultipartResolver() {
        return new StandardServletMultipartResolver();
    }

    // 开启静态资源访问
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//         <mvc:default-servlet-handler/>
        configurer.enable();
    }
}